package com.dz.controller.admin;

import com.dz.constant.SystemConstant;
import com.dz.dto.SetmealDTO;
import com.dz.dto.SetmealPageQueryDTO;
import com.dz.result.Result;
import com.dz.service.SetmealService;
import com.dz.vo.PageVo;
import com.dz.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 鄧浩奇
 * @classname SetMealController
 * @description
 * @since 2023/12/28 19:33
 **/
@Api(tags = "套餐相关接口")
@RestController
@RequestMapping("/admin/setmeal")
public class SetMealController {

    @Resource
    private SetmealService setmealService;


    @ApiOperation("新增套餐")
    @PostMapping()
    public Result<String> insertSetmeal(@RequestBody SetmealDTO setmealDTO) {
        return setmealService.insertSetmeal(setmealDTO);
    }


    @ApiOperation("套餐分页")
    @GetMapping("/page")
    public Result<PageVo> page(SetmealPageQueryDTO queryDTO) {
        PageVo pageVo = setmealService.page(queryDTO);
        return Result.success(pageVo);
    }


    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }


    @ApiOperation("修改套餐")
    @PutMapping()
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        return setmealService.update(setmealDTO);
    }


    @ApiOperation("根据id（批量）删除套餐")
    @DeleteMapping()
    public Result<String> deleteByIds(String ids) {
        List<Long> idsIsLong = new ArrayList<>();
        Arrays.stream(ids.split(SystemConstant.SPLIT)).forEach(id -> idsIsLong.add(Long.parseLong(id)));
        return setmealService.deleteBatch(idsIsLong);
    }


    @ApiOperation("套餐的起售、停售")
    @PostMapping("/status/{status}")
    public Result<String> changeStatus(Long id, @PathVariable Integer status) {
        // 0:停用 1:启用
        return setmealService.changeStatus(id,status);
    }
}
